package com.by.yz.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.baidu.platform.comapi.map.E;
import com.by.yz.R;
import com.by.yz.Register.RegisterRESTFul;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.activity.regitser.RegisterInfoActivity;
import com.by.yz.entity.Password;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.PasswordRESTFul;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.loginUtil.codeRESTFul;
import com.by.yz.util.ToJSonObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by mrs yang on 2016/10/16.
 */
public class ForgetPassword extends BaseActivity {
    private EditText phone;
    private String CODE = "";
    private EditText register_vercode;//填写验证码
    private Button getCode;//获取验证码
    private EditText pwd;
    private Button finish;
    private String phoneNumber;
    Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forget_password);
        phone=(EditText)findViewById(R.id.phonenum);
        register_vercode = (EditText) findViewById(R.id.vercode);//填写验证码
        getCode = (Button) findViewById(R.id.forget_getCode);//获取验证码
        finish = (Button) findViewById(R.id.forget_finish);//完成
        pwd = (EditText) findViewById(R.id.editText);//忘记密码

        //计时初始化
        final timeCount timecount = new timeCount(60000, 1000);
        getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phoneNumber=phone.getText().toString();
                Log.e("ggg",String.valueOf(phoneNumber.length()));
                if(phoneNumber.length()!=11){
                    new MaterialDialog.Builder(ForgetPassword.this).content("手机号码非法,请重新输入").positiveText("确定").show();
                }else{
                    AccountRESTFul accountRESTFul=new AccountRESTFul();
                    accountRESTFul.get(new AccountRESTFul.IhttpCb() {
                        @Override
                        public void ok(JSONObject response) {
                            Log.e("sss","授权 成功");
                            UserRESTFul userRESTFul=new UserRESTFul();
                            Log.e("ggg","手机号码"+phoneNumber);
                            userRESTFul.getAccout(phoneNumber, new UserRESTFul.UserPOSTCallBack() {
                                @Override
                                public void ok(JSONObject response) throws JSONException {

                                    codeRESTFul getcodeRequest = new codeRESTFul();
                                    getcodeRequest.getCode(ForgetPassword.this, phoneNumber, new codeRESTFul.IgetCodeCb() {
                                        @Override
                                        public void ok(JSONArray response) {
                                            getCode.setEnabled(false);
                                            timecount.start();
                                            Gson gson = new Gson();
                                            List<String> list = gson.fromJson(response.toString(), new TypeToken<List<String>>() {
                                            }.getType());
                                            CODE = list.get(0);
                                            if (CODE == register_vercode.getText().toString()) {
                                                Log.e("ss", "获取code成功");
                                            }
                                        }

                                        @Override
                                        public void error(String error) {
                                            final MaterialDialog registerSuccess = new MaterialDialog.Builder(ForgetPassword.this)
                                                    .content("获取验证码失败！")
                                                    .show();
                                            Handler handler = new Handler();
                                            handler.postDelayed(new Runnable() {
                                                public void run() {
                                                    registerSuccess.dismiss();//设置弹框消失
                                                }
                                            }, 1000);
                                        }
                                    } );
                                }

                                @Override
                                public void error(String response) {
                                    final MaterialDialog registerSuccess = new MaterialDialog.Builder(ForgetPassword.this)
                                            .content("您还没注册,请先注册！")
                                            .show();
                                    Handler handler = new Handler();
                                    handler.postDelayed(new Runnable() {
                                        public void run() {
                                            registerSuccess.dismiss();//设置弹框消失
                                        }
                                    }, 1000);
                                }
                            });
                        }
                        @Override
                        public void error(String error) {
                            Log.e("eee","授权失败");
                        }
                    });
                }
            }
        });
        //完成操作
        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String pword=pwd.getText().toString();
                String code=register_vercode.getText().toString();
                Password password=new Password();
                password.setLogin(phoneNumber);
                password.setNewPassword(pword);
                if((pword!=null)&&(code!=null)){
                    JSONObject jsonObject = ToJSonObject.beanToJSONObejct(password);
                    PasswordRESTFul passwordRESTFul=new PasswordRESTFul();
                    passwordRESTFul.postPassword(phoneNumber,pword, jsonObject, new PasswordRESTFul.PostPwd() {
                        @Override
                        public void ok(JSONObject response) throws JSONException {
                            final MaterialDialog registerSuccess = new MaterialDialog.Builder(ForgetPassword.this)
                                    .content("修改密码成功")
                                    .show();
                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                public void run() {
                                    //execute the task
                                    registerSuccess.dismiss();
                                    intent.setClass(ForgetPassword.this, LoginActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            }, 1000);
                        }

                        @Override
                        public void error(String error) {

                        }
                    });
                }
                else {
                    new MaterialDialog.Builder(ForgetPassword.this)
                            .content("请输入密码")
                            .positiveText("确定")
                            .show();
                }
            }
        });
//        finish.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                final Intent intent = getIntent();
//                String pword=pwd.getText().toString();
//                String code=register_vercode.getText().toString();
//                Password password=new Password();
//                password.setLogin(phoneNumber);
//                password.setNewPassword(pword);
//                if((pword!=null)&&(code!=null)) {
//                    JSONObject jsonObject = ToJSonObject.beanToJSONObejct(password);
//                    PasswordRESTFul passwordRESTFul = new PasswordRESTFul();
//                    passwordRESTFul.postPassword(phoneNumber,jsonObject, new PasswordRESTFul.PostPwd() {
//                        @Override
//                        public void ok(JSONObject response) throws JSONException {
//                            final MaterialDialog registerSuccess = new MaterialDialog.Builder(ForgetPassword.this)
//                                            .content("修改密码成功")
//                                            .show();
//                            Handler handler = new Handler();
//                                    handler.postDelayed(new Runnable() {
//                                        public void run() {
//                                            //execute the task
//                                            registerSuccess.dismiss();
//                                            intent.setClass(ForgetPassword.this, LoginActivity.class);
//                                            startActivity(intent);
//                                            finish();
//                                        }
//                                    }, 1000);
//                        }
//                        @Override
//                        public void error(String error) {
//                            // 授权失败
//                            Log.e("GG", "授权失败");
//                        }
//                    });
//                }else{
//                    new MaterialDialog.Builder(ForgetPassword.this)
//                                    .content("请输入密码")
//                                    .positiveText("确定")
//                                    .show();
//                }
//            }
//        });

    }
    //计时内部类
    class timeCount extends CountDownTimer {
        public timeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onFinish() {// 计时完毕
            getCode.setText("重新获取验证码");
            getCode.setEnabled(true);
            getCode.setTextColor(Color.parseColor("#333333"));
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            getCode.setEnabled(false);;//防止重复点击
            getCode.setText(millisUntilFinished / 1000 + "秒重新获取");
            getCode.setTextColor(Color.parseColor("#999999"));
        }
    }
}
